<template>
  <div class="wrapper">
    <div class="login-wrap">
      <img
        class="logo"
        src="../../../static/img/logo.png"

      />
      <div class="ms-login">
        <div class="ms-title">智能会议助手后台管理系统</div>
        <el-form label-width="0px" class="ms-content">
          <el-form-item prop="username">
            <el-input v-model="usn" placeholder="用户名">
              <el-button slot="prepend" icon="icon icon-geren"></el-button>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input type="password" placeholder="密码" v-model="psw">
              <el-button slot="prepend" icon="icon icon-lock-line"></el-button>
            </el-input>
          </el-form-item>
          <el-form-item class="rememberMe">
            <label class="checkbox-inline">
              <el-checkbox
                id="isRemberMe"
                name="isRemberMe"
                v-model="isRemberMe"
                type="checkbox"
                ><p class="login-tips">记住我</p></el-checkbox
              >
            </label>
          </el-form-item>
          <div class="login-btn">
            <el-button type="primary" @click="sub()">登录</el-button>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import md5 from "js-md5";
import axios from "axios";
export default {
  name: "Login",
  data() {
    return {
      // psw: 'aaa111',
      // usn: '15608496960',
      psw: "",
      usn: "",
      isRemberMe: null
    };
  },
  methods: {
    check: function() {
      if (this.usn === "" || this.psw === "") {
        return false;
      } else {
        return true;
      }
    },
    sub: function() {
      if (this.check()) {
        this.login();
      } else {
        this.$message.error("请输入正确的用户名或密码!");
      }
    },
    login: function() {
      const formdata = new FormData();
      formdata.append("username", this.usn);
      formdata.append("password", md5(this.psw));
      formdata.append("isRemberMe", true);
      axios
        .post("http://119.23.110.183:8080/login", formdata)
        .then(this.loginSucc);
    },
    loginSucc: function(res) {
      res = res.data;
      let _this = this;
      if (res.code === 0 && res.data) {
        const data = res.data;
        _this.$store.commit("changeToken", data.token);
        if (_this.$route.query.redirect) {
          // 如果存在参数
          let redirect = _this.$route.query.redirect;
          _this.$router.push(redirect); // 则跳转至进入登录页前的路由
        } else {
          _this.$router.push("/"); // 否则跳转至首页
        }
      } else if (res.code === -1) {
        alert(res.msg);
      }
    }
  }
};
</script>

<style scoped>
.wrapper {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  width: 100%;
  background-size: cover;
  background-image: url("../../../static/img/loginbg.jpg");
}
.login-wrap {
  position: absolute;
  left: 50%;
  top: 0;
  width: 350px;
  margin-left: -175px;
}
.ms-title {
  width: 100%;
  line-height: 50px;
  text-align: center;
  font-size: 20px;
  color: #fff;
  border-bottom: 1px solid #ddd;
}
.ms-login {
  border-radius: 5px;
  background: rgba(255, 255, 255, 0.3);
  overflow: hidden;
}
.ms-content {
  padding: 30px 30px;
}
.login-btn {
  text-align: center;
}
.login-btn button {
  width: 100%;
  height: 36px;
  margin-bottom: 10px;
}
.login-tips {
  font-size: 12px;
  line-height: 30px;
  color: #fff;
}
.logo {
  position: relative;
  width: 210px;
  left: 42%;
  margin-left: -75px;
}
#isRemberMe {
  boder: 1px solid #ddd;
  width: 14px;
  height: 14px;
}
</style>
